module.exports = {
	/**
	 * 添加单条数据
	 * @url admin/kong/sys/add 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @param {Number} code 错误码，0表示成功
	 * @param {String} msg 详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: 'ok' };
		// 业务逻辑开始-----------------------------------------------------------
		let {

			client,
			client_name,
			detection_type,
			submitUser,
			detection_category,
			detection_standard_name,
			detection_standard,
			detection_user,
			sample_id,
			sampleno,
			samplename,
			sample_save_info,
			sample_keeptime,
			sample_troptime,
			sample_list,
			detect_time,
			type,
			sample_status,
		} = data;
		// 这里需要把 params1 params2 params3 改成你数据库里允许用户添加的字段
		let dbName = "tm-detection-form",
			info = await vk.baseDao.findByWhereJson({
				dbName: "uni-id-users",
				whereJson: {
					nickname: detection_user,
				}
			}),
			_client_price = await vk.baseDao.findByWhereJson({
				dbName: "tm-client-price",
				whereJson: {
					client_id: client,
					category_id: detection_category
				}
			}),
			_price = 0,
			detection_user_sign = undefined;
		if (vk.pubfn.isNotNull(info) && vk.pubfn.isNotNull(info.sign_image)) {
			detection_user_sign = info.sign_image
		}


		res.id = await vk.baseDao.add({
			dbName,
			dataJson: {
				no: vk.pubfn.createOrderNo("", 17),
				detect_time,
				client,
				client_name,
				detection_type,
				submitUser,
				detection_category,
				detection_standard_name,
				detection_standard,
				detection_user,
						sample_id,
			sampleno,
			samplename,
				sample_save_info,
				sample_keeptime,
				sample_troptime,
				type,
				sample_status,

				detection_user_sign: detection_user_sign
			}
		});
		
		if (sample_list != undefined && sample_list.length > 0) {
			if (vk.pubfn.isNotNull(_client_price) && vk.pubfn.isNotNull(_client_price.price))
			_price = vk.pubfn.string2Number(_client_price.price);
			let _add_sample_list = sample_list.map(x => {
				return {
					'detectionform_id': res.id,
					'client': client,
					'client_name': client_name,
					'detection_user': detection_user,
					'price': _price,
					...x
				}
			})
			let ids = await vk.baseDao.adds({
				dbName: "tm-sample-testing",// 表名
				dataJson: _add_sample_list
			});
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
